package pl.webapps.alarmme;

import java.io.File;

import android.util.Log;

public class LocalResourcesHandler {
    private static final String PATH_TO_APPLICATION_RESOURCES = "/sdcard/AlarmMe/";
    private static final String PATH_TO_SOUNDS = PATH_TO_APPLICATION_RESOURCES + "sounds/";
    private static final String SOUND_EXTENSION = ".wav";
    private static final String LOG_TAG = "LocalResourcesHandler";

    public static boolean checkIfSdCardPresentAndHaveSpace() {
        return true;
    }

    /**
     * 
     * @param repoName
     *            folder where files will be stored under sounds folder
     * @param fileName
     *            name of the current file
     */
    public static String getSoundFromResource(String repoName, String soundName) {
        String fullPath = PATH_TO_SOUNDS + repoName + "/" + soundName + SOUND_EXTENSION;
        Log.d(LOG_TAG, "getSoundFromResource fullPath=" + fullPath);
        File f = new File(fullPath);
        if (f.exists()) {
            return fullPath;
        } else {
            return null;
        }
    }

    /**
     * 
     * @param repoName
     *            folder where files will be stored under sounds folder
     * @param fileName
     *            name of the current file
     */
    public static void saveSoundStreamToFile(String repoName, String fileName) {
        try {
            File dir = new File(PATH_TO_SOUNDS + repoName + "/");
            if (!dir.exists()) {
                dir.mkdirs();
            }
            File f = new File(PATH_TO_SOUNDS + repoName + "/" + fileName + SOUND_EXTENSION);
            if (!f.exists()) {
                f.createNewFile();
            }
            // FileOutputStream out = new FileOutputStream(f);
            // bmp.compress(Bitmap.CompressFormat.PNG, 90, out);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
    // public static void saveAlarmsToFile(List<PendingIntentWrapper> items) {
    // try {
    // File dir = new File(PATH_TO_APPLICATION_RESOURCES);
    // if (!dir.exists()) {
    // dir.mkdirs();
    // }
    // File f = new File(PATH_TO_APPLICATION_RESOURCES + ALARMS_FILE);
    // if (!f.exists()) {
    // f.createNewFile();
    // }
    // ObjectOutputStream oos = new ObjectOutputStream(new FileOutputStream(f));
    // oos.writeObject(items);
    // } catch (Exception e) {
    // e.printStackTrace();
    // }
    // }
    //
    // @SuppressWarnings("unchecked")
    // public static List<PendingIntentWrapper> loadAlarmsFromFile() {
    // File f = new File(PATH_TO_APPLICATION_RESOURCES + ALARMS_FILE);
    // if (!f.exists()) {
    // return null;
    // }
    // ObjectInputStream ois;
    // try {
    // ois = new ObjectInputStream(new FileInputStream(f));
    // Object obj = ois.readObject();
    // return (List<PendingIntentWrapper>) obj;
    // } catch (StreamCorruptedException e) {
    // // TODO Auto-generated catch block
    // e.printStackTrace();
    // } catch (FileNotFoundException e) {
    // // TODO Auto-generated catch block
    // e.printStackTrace();
    // } catch (IOException e) {
    // // TODO Auto-generated catch block
    // e.printStackTrace();
    // } catch (ClassNotFoundException e) {
    // // TODO Auto-generated catch block
    // e.printStackTrace();
    // }
    // return null;
    // }
}
